/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaCargo.java
 *
 * Created on 19/11/2011, 08:53:53 AM
 */

package Vistas;

import Beans.Cargo;
import Controladores.MCargo;
import Imagenes.CtrlImagen;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ryodans
 */
public class VistaCargo extends javax.swing.JFrame {

    /** Creates new form VistaCargo */
    DefaultTableModel dtm = new DefaultTableModel();
    MCargo Mcar = new MCargo();
    Cargo c = new Cargo();
    int indice=-1;
    int fila;
    public VistaCargo() {
        initComponents();
    }
    
    public VistaCargo(MCargo Mcar) {
        initComponents();
        this.Mcar = Mcar;
        this.jTNombre.requestFocus();
        dtm.addColumn("Cargo");
        dtm.addColumn("Salario");

        for(int x=0;x<Mcar.getListaCargo().size();x++){
            c = Mcar.getListaCargo().get(x);
            Vector<String> vc = new Vector<String>();
            vc.add(c.getNombre());
            vc.add(c.getSalario()+"");
            dtm.addRow(vc);
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new CtrlImagen("/Imagenes/a1.jpg");
        jPanelTransparente1 = new Imagenes.JPanelTransparente();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTNombre = new javax.swing.JTextField();
        jTSalario = new javax.swing.JTextField();
        jPanelTransparente5 = new Imagenes.JPanelTransparente();
        JBStop = new javax.swing.JButton();
        jBModificar = new javax.swing.JButton();
        jBEliminar = new javax.swing.JButton();
        jBRegistrar = new javax.swing.JButton();
        jBSalir1 = new javax.swing.JButton();
        jPanelTransparente3 = new Imagenes.JPanelTransparente();
        jBEliminarTodo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Vista Interfaz Manejo de Cargos");

        jLabel1.setText("Nombre");

        jLabel2.setText("Salario");

        jTSalario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTSalarioKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanelTransparente1Layout = new javax.swing.GroupLayout(jPanelTransparente1);
        jPanelTransparente1.setLayout(jPanelTransparente1Layout);
        jPanelTransparente1Layout.setHorizontalGroup(
            jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                    .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTSalario, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelTransparente1Layout.setVerticalGroup(
            jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        JBStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/burn.png"))); // NOI18N
        JBStop.setText("Cancelar");
        JBStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBStopActionPerformed(evt);
            }
        });

        ListSelectionModel lsm = jTable1.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel ls=(ListSelectionModel )e.getSource();
                    if(ls.isSelectionEmpty()){
                        fila=-1;
                        jBModificar.setEnabled(false);
                    }else{
                        fila=ls.getMinSelectionIndex();
                        jBModificar.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jBModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/edit.png"))); // NOI18N
        jBModificar.setText("MODIFICAR");
        jBModificar.setEnabled(false);
        jBModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBModificarActionPerformed(evt);
            }
        });

        ListSelectionModel lsms = jTable1.getSelectionModel();
        lsms.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel lss=(ListSelectionModel )e.getSource();
                    if(lss.isSelectionEmpty()){
                        fila=-1;
                        jBEliminar.setEnabled(false);
                    }else{
                        fila=lss.getMinSelectionIndex();
                        jBEliminar.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jBEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/stop.png"))); // NOI18N
        jBEliminar.setText("ELIMINAR");
        jBEliminar.setEnabled(false);
        jBEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarActionPerformed(evt);
            }
        });

        jBRegistrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Add.png"))); // NOI18N
        jBRegistrar.setText("AGREGAR");
        jBRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRegistrarActionPerformed(evt);
            }
        });

        jBSalir1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Desktop 2.png"))); // NOI18N
        jBSalir1.setText("SALIR");
        jBSalir1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalir1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelTransparente5Layout = new javax.swing.GroupLayout(jPanelTransparente5);
        jPanelTransparente5.setLayout(jPanelTransparente5Layout);
        jPanelTransparente5Layout.setHorizontalGroup(
            jPanelTransparente5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTransparente5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBRegistrar, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBModificar, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(JBStop, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBSalir1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelTransparente5Layout.setVerticalGroup(
            jPanelTransparente5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBRegistrar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(JBStop, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBSalir1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jBEliminarTodo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/stop.png"))); // NOI18N
        jBEliminarTodo.setText("ELIMINAR TODO");
        jBEliminarTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarTodoActionPerformed(evt);
            }
        });

        jTable1.setModel(dtm);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanelTransparente3Layout = new javax.swing.GroupLayout(jPanelTransparente3);
        jPanelTransparente3.setLayout(jPanelTransparente3Layout);
        jPanelTransparente3Layout.setHorizontalGroup(
            jPanelTransparente3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTransparente3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBEliminarTodo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                .addGap(25, 25, 25))
        );
        jPanelTransparente3Layout.setVerticalGroup(
            jPanelTransparente3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelTransparente3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jBEliminarTodo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTransparente1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanelTransparente5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                .addComponent(jPanelTransparente3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTransparente3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanelTransparente1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanelTransparente5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jMenu1.setText("Archivo");

        jMenuItem1.setText("Salir");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Operaciones");

        jMenuItem2.setText("Modificar");
        jMenu2.add(jMenuItem2);

        jMenuItem3.setText("Eliminar");
        jMenu2.add(jMenuItem3);

        jMenuItem4.setText("Actualizar");
        jMenu2.add(jMenuItem4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JBStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBStopActionPerformed
        //Cancelar
        this.Actualizar();
}//GEN-LAST:event_JBStopActionPerformed

    private void jBModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBModificarActionPerformed
        // Modificar Categoria
        if(jBModificar.getText().equals("MODIFICAR")){
            indice = jTable1.getSelectedRow();
            c = Mcar.getListaCargo().get(indice);
            jTNombre.setText(c.getNombre());
            jTNombre.requestFocus();
            jTSalario.setText(c.getSalario()+"");
            jBModificar.setText("GUARDAR");
            ImageIcon[] img_Btns = new ImageIcon[8];
            img_Btns[0] = new ImageIcon(getClass().getResource("/Imagenes/Floppy.png"));
            jBModificar.setIcon(img_Btns[0]);

        }else{
            Cargo carg=new Cargo(jTNombre.getText(),Double.parseDouble(jTSalario.getText()));
            Mcar.getListaCargo().set(indice, carg);
            //Mca.Modificar(indice, c.getCodigo(), c);
            indice=-1;
            c=new Cargo();

            Actualizar();
        }
}//GEN-LAST:event_jBModificarActionPerformed

    private void jBEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarActionPerformed
        // Eliminar
        Cargo cr=Mcar.getListaCargo().get(jTable1.getSelectedRow());
        Mcar.Eliminar(cr.getNombre());
        Actualizar();
    }//GEN-LAST:event_jBEliminarActionPerformed

    private void jBRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRegistrarActionPerformed
        //Registrar
        if(jTNombre.getText().equals("") || jTSalario.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Porfavor, Rellene todo los Campos","Error",JOptionPane.ERROR_MESSAGE);
        }else{
            Cargo Ca = new Cargo();
            Ca.setNombre(jTNombre.getText());
            Ca.setSalario(Double.parseDouble(jTSalario.getText()));
            Mcar.Registrar(Ca);
            AgregarLineaTabla(Mcar.getListaCargo());
            jTNombre.requestFocus();
            this.jTNombre.setText("");
            this.jTSalario.setText("");
        }
        


    }//GEN-LAST:event_jBRegistrarActionPerformed

    private void jBSalir1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalir1ActionPerformed
        // // Salir
        this.dispose();
}//GEN-LAST:event_jBSalir1ActionPerformed

    private void jBEliminarTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarTodoActionPerformed
        // Eliminar Todo
        //this.cleanTabla();
    }//GEN-LAST:event_jBEliminarTodoActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // Salir
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jTSalarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTSalarioKeyTyped
        // Solo numeros
        char c = evt.getKeyChar();
        if(Character.isDigit(c)==false){
            evt.consume();
        }
    }//GEN-LAST:event_jTSalarioKeyTyped

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaCargo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBStop;
    private javax.swing.JButton jBEliminar;
    private javax.swing.JButton jBEliminarTodo;
    private javax.swing.JButton jBModificar;
    private javax.swing.JButton jBRegistrar;
    private javax.swing.JButton jBSalir1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private Imagenes.JPanelTransparente jPanelTransparente1;
    private Imagenes.JPanelTransparente jPanelTransparente3;
    private Imagenes.JPanelTransparente jPanelTransparente5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTNombre;
    private javax.swing.JTextField jTSalario;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void Actualizar() {
        this.dispose();
        VistaCargo Vc = new VistaCargo(Mcar);
        Vc.setLocationRelativeTo(null);
        Vc.setVisible(true);
    }

    public void borrarLista(){
        Mcar.getListaCargo().clear();
        try {
            Mcar.grabar();
        } catch (IOException ex) {
            Logger.getLogger(VistaCargo.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

       private void AgregarLineaTabla(List<Cargo> Cargos) {
        this.cleanTabla();

        for(int x=0;x<Cargos.size();x++){
            Vector<String> v = new Vector<String>();
            v.add(Cargos.get(x).getNombre());
            v.add(Cargos.get(x).getSalario()+"");
            dtm.addRow(v);
        }

    }
//
    public void cleanTabla(){

        dtm=(DefaultTableModel) jTable1.getModel();
        int x=dtm.getRowCount();
        for(int i=-1;i<(x-1);i++){
              dtm.removeRow(0);
        }
    }
}
